<!--  -->
<template>
  <div class='settings'>
    <!--        头部-->
    <head-top></head-top>
    <div style="width: 100%;height: 50px;"></div>
    <div class="settingsCon">
      <van-form validate-first @submit="onSubmit(form)">
        <div class="formItem">
          <div class="formItemBg padg">
            <p class="nameTitle">头像</p>
            <van-field v-model="form.avatar">
              <template #input>
                <van-uploader :after-read="beforeRead">
                  <van-button type="primary">上传</van-button>
                </van-uploader>
                <img :src="form.avatar"/>
              </template>
            </van-field>
          </div>
        </div>
        <div class="formItem">
          <div class="formitemPd">
            <van-field
                v-model="form.nickName"
                name="用户名"
                label="昵称"
                placeholder="用户名"
            />
          </div>
        </div>
        <div class="formItem">
          <div class="formitemPd">

            <van-field
                v-model="form.password"
                name="密码"
                label="密码"
                type="password"
                placeholder="请输入"
                id="passw"
                @focus="clickipt"
            />
          </div>
        </div>
        <div class="sub">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from ‘《组件路径》‘;
import headTop from "../../components/base/headTop.vue"
import {rules} from '@/utils/common'
import comApi from '@/api/comapi'
import user from '@/api/user'
import {Toast} from 'vant';

export default {
//import引入的组件需要注入到对象中才能使用
  components: {
    headTop
  },
  data() {

    return {
      srcimg: '',
      previewFlag: true,
      uploader: [],
      fileType: ['image/png', 'image/jpeg'],
      form: {
        avatar: "",
        nickName: "",
        password: ""
      },
      rules: rules,
    };
  },
//监听属性 类似于data概念
  computed: {},
//监控data中的数据变化
  watch: {},

  methods: {
    myInfoLoad() {
      user.myInfo().then(res => {
        this.form = res.data.record
        console.log(this.form)
      })
    },
    // 返回布尔值
    beforeRead(file) {
      console.log(file)
      console.log(file.file.type)

      if (this.fileType.indexOf(file.file.type) < 0) {
        Toast('请上传 jpg 格式图片');
        return false;
      }
      this.avatar = file.file.type
      let formData = new FormData();
      console.log(formData)
      formData.append("file", file.file)
      comApi.fileUpload(formData).then(res => {
        console.log(res)
        this.form.avatar = res.data
      })
      return true;
    },

    onSubmit(values) {
      console.log('submit', values);
     if(!/^[a-zA-Z0-9]{8,20}$/.test(values.password)){
        Toast('密码至少包含字母，数字，且不少于8位');
        return;
     }
      var aa = document.getElementById('passw')
      aa.setAttribute('type','password')

      let json = {
        "avatar": values.avatar,
        "nickName": values.nickName,
        "password": values.password

      }
      user.updateInfo(json).then(res => {
        Toast('设置成功');
      })
    },

    // 输入框焦点触发
    clickipt(){
      this.form.password = ''
      var aa = document.getElementById('passw')
      aa.removeAttribute('type')
    }
  },

  created() {
    this.myInfoLoad()
  },

  mounted() {

  },
  beforeCreate() {
  },
  beforeMount() {
  },
  beforeUpdate() {
  },
  updated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  activated() {
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="less" scoped>
.settings {
  width: 100%;
  min-height: 100%;
  background: #f8f8f8;
}

.settingsCon {
  width: 100%;

  .van-cell {
    background: #f8f8f8;
  }

  .formItem {
    width: 100%;
    border-bottom: 1px solid #e5e5e6;

    .uploaderCon {
      display: flex;
      align-items: center;
      padding: 0px 10px;

      /deep/ .van-field__label {
        color: #9e989f !important;
      }

      .leftText {
        width: 86px;
        color: #131413;
      }

      .uploaderField {
        flex: 1;
        position: relative;

        .imgs {
          .uploadBut {
            width: 50px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            padding: 0 20px;
            margin: 0 0 15px 30px;
            color: white;
            background: #f8f5be;
            border-radius: 20px;
            position: relative;
            overflow: hidden;

            input {
              width: 100%;
              height: 100%;
              position: absolute;
              top: 2px;
              left: -10px;
              opacity: 0;
            }

          }

          .preview {
            width: 75px;
            height: 75px;
            margin-left: 20px;
            position: relative;

            img {
              width: 75px;
              height: 75px;
              border-radius: 50%;
              border: none;
              outline: none;
            }

            .bg {
              position: absolute;
              left: 0;
              top: 0;
              width: 100%;
              height: 100%;
              background: #f8f8f8;
            }

          }
        }
      }
    }

    .formItemBg {
      display: flex;
      align-items: center;
      flex-wrap: nowrap;
      padding: 10px;
      line-height: 30px;

      /deep/ .van-field {
        flex: 1;
      }

      /deep/ .van-button--primary {
        background: #f8f5be;
        border: none;
        border-radius: 20px;
        height: 30px;
        line-height: 30px;
        color: black;
      }

      /deep/ .van-button--normal {
        padding: 5px 30px;
      }

      /deep/ .van-uploader__input-wrapper {
        left: 20px;
        position: fixed;
        top: 60px;
        left: 100px;
      }

      .ipt {
        flex: 1;
        padding-left: 20px;

        .select {
          width: 100%;
          height: 30px;
          border: none;

          option {
            height: 30px;
          }
        }
      }

      /deep/ .van-cell {
        padding: 0px;

        input {
          text-indent: 20px;
          height: 30px;
        }
      }

    }

    .padg {
      padding-top: 50px;

      .nameTitle {
        margin-right: 20px;
      }

      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
      }
    }
  }

  .sub {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #ffffff;
    padding-bottom: 10px;


    .van-button {
      width: 95%;
      margin: 0 auto;
      background: #f8f5be;
      border: none;
      color: black;
    }
  }
}

</style>
